//
//  RNOLMeViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/8.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLMeViewController.h"
#import "RNOLSettingsViewController.h"
#import "RNOLMyMessageViewController.h"
#import "RNOLFundsDetailTabBarController.h"
#import "RNOLBasicInfoViewController.h"
#import "RNOLMyFriendViewController.h"
#import "RNOLMeHeader.h"
#import "RNOLMeNavgationBar.h"
#import "RNOLMeItemMoel.h"
#import "RNOLMeClaimTransferViewController.h"
#import "RNOLAutoBiddingViewController.h"
#import "RNOLMyGiftListViewController.h"
#import "RNOLMyRewardsViewController.h"
#import "RNOLMyBankCardViewController.h"
#import "RNOLMyInvestSubViewController.h"
#import "RNOLMyBackRefundCalendarViewController.h"
#import "RNOLRechargeViewController.h"
#import "RNOLWithdrawViewController.h"
#import "RNOLMyBorrowMoneyViewController.h"
#import "RNOLMeLoginCoverView.h"


@interface RNOLMeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) RNOLMeNavgationBar *navgationBar;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) RNOLMeHeader *header;
@property (nonatomic,strong) NSDictionary *dataDict;
@property (nonatomic,strong) RNOLMeLoginCoverView *loginCoverView;

@end

@implementation RNOLMeViewController

- (RNOLMeLoginCoverView *)loginCoverView{
    if (!_loginCoverView) {
        _loginCoverView = [[RNOLMeLoginCoverView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview: _loginCoverView];
    }
    return _loginCoverView;
}

- (RNOLMeNavgationBar *)navgationBar{
    if (!_navgationBar) {
        _navgationBar = [RNOLMeNavgationBar FromXIB];
    }
    UIView *bgView = [RNOLUnitTool createViewWithBackgroundColor:KWhiteColor];
    bgView.frame = CGRectMake(0, kStatusBarHeight, KWIDTH , kNavBarHeight - kStatusBarHeight);
    _navgationBar.frame = bgView.bounds;
    [bgView addSubview:_navgationBar];
    [self.view addSubview:  bgView];
    return _navgationBar;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarHeight, KWIDTH , KHIGHT-kTabBarHeight-kNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        RNOLMeHeader *header = [[RNOLMeHeader alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 310)];
        self.header = header;
        _tableView.tableHeaderView = header;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"我的"];
    self.view.backgroundColor = kAppMainColor ;
    [self tableView];
    [self navgationBar];
    [self loadLocalDataSource];
    [self doEventsAction];
//    [self.view bringSubviewToFront: self.loginCoverView]; //判断未登陆才加上去
    
}

- (void)loadLocalDataSource{
     self.dataDict = [RNOLMeItemMoel getLoacalDataSourceFromPlistFile];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [RNOLLoginTool setNavgationBarNeedAnimation:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataDict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.dataDict[@(section).stringValue];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    }
    
    RNOLMeItemMoel *item = [RNOLMeItemMoel yy_modelWithDictionary: self.dataDict[@(indexPath.section).stringValue][indexPath.row]];
    cell.textLabel.text = item.title;
    cell.textLabel.font = KFontSize(14);
    cell.textLabel.textColor = kTextColor;
    cell.imageView.image = [UIImage imageNamed: item.icon];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 1){
        return [UIView new];
    }
    UIView *bgView = [RNOLUnitTool createViewWithBackgroundColor:kBgColor];
    bgView.frame = CGRectMake(0, 0, KWIDTH , 5);
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 0.01;
    }
    return 5;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RNOLMeItemMoel *item = [RNOLMeItemMoel yy_modelWithDictionary: self.dataDict[@(indexPath.section).stringValue][indexPath.row]];
    NSString *title = item.title;
    if (kCompareTwoString(title, @"我要借款")) {
        [self pushPageWithClass:[RNOLMyBorrowMoneyViewController class]];
    }else if (kCompareTwoString(title, @"债权转让")) {
        [self pushPageWithClass:[RNOLMeClaimTransferViewController class]];
    }else if (kCompareTwoString(title, @"自动投标")) {
        [self pushPageWithClass:[RNOLAutoBiddingViewController class]];
    }else if (kCompareTwoString(title, @"我的好友")) {
        [self pushPageWithClass:[RNOLMyFriendViewController class]];
    }else if (kCompareTwoString(title, @"我的奖励")) {
        [self pushPageWithClass:[RNOLMyRewardsViewController class]];
    }else if (kCompareTwoString(title, @"我的礼品")) {
        [self pushPageWithClass:[RNOLMyGiftListViewController class]];
    }else if (kCompareTwoString(title, @"系统设置")) {
        [self pushPageWithClass:[RNOLSettingsViewController class]];
    }
}

#pragma makr- 事件处理
- (void)doEventsAction{
    @weakify(self);
    [self.navgationBar setHeaderClickBlock:^{//头像
        @strongify(self);
        [self pushPageWithClass:[RNOLBasicInfoViewController class]];
    }];

    [self.navgationBar setMessageBlock:^{//消息
        @strongify(self);
        [self pushPageWithClass:[RNOLMyMessageViewController class]];
    }];
    
    [self.header setIndexBlock:^(NSInteger index) {//我的投资,回款日历,银行卡,资金明细
        @strongify(self);
        NSArray *vcClassArray = @[[RNOLMyInvestSubViewController class],[RNOLMyBackRefundCalendarViewController class],[RNOLMyBankCardViewController class], [RNOLFundsDetailTabBarController class]];
        [self pushPageWithClass: vcClassArray[index]];
    }];
    
    [self.header setRechargeBlock:^{//充值
        @strongify(self);
        [self pushPageWithClass:[RNOLRechargeViewController class]];
    }];
    
    [self.header setWithdrawBlock:^{//提现
        @strongify(self);
        [self pushPageWithClass:[RNOLWithdrawViewController class]];
    }];
    
}

@end
